package com.jiechuang.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiechuang.common.bean.Permission;
import com.jiechuang.common.bean.addOrUpdate.PermissionAddOrUpdate;
import com.jiechuang.common.exception.AddException;
import com.jiechuang.common.exception.DeleteException;
import com.jiechuang.common.exception.UpdateException;
import com.jiechuang.user.dto.PermissionQueryInfo;
import com.jiechuang.user.mapper.PermissionMapper;
import com.jiechuang.user.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public PageInfo<Permission> getPermissionList(PermissionQueryInfo permissionQueryInfo) {
        PageHelper.startPage(permissionQueryInfo.getPageNum(), permissionQueryInfo.getPageSize());
        List<Permission> permissionList=permissionMapper.getPermissionList(permissionQueryInfo);
        if(permissionQueryInfo.getType()==2){
             permissionList = getPermissionClassList(permissionList,permissionQueryInfo.getMaxPermissionLevel());
        }
        return new PageInfo<>(permissionList);
    }

    @Override
    public void addPermission(PermissionAddOrUpdate userAddOrUpdate) {
        int i=permissionMapper.addPermission(userAddOrUpdate);
        if(i==0){
            throw new AddException("新增失败");
        }
    }

    @Override
    public Permission getPermission(Integer id) {
        return permissionMapper.getPermission(id);
    }

    @Override
    public void updatePermission(PermissionAddOrUpdate userAddOrUpdate) {
        int i=permissionMapper.updatePermission(userAddOrUpdate);
        if(i==0){
            throw new UpdateException("修改失败");
        }
    }

    @Override
    public void deletePermission(Integer id) {
        int i=permissionMapper.deletePermission(id);
        if(i==0){
            throw new DeleteException("删除失败");
        }
    }

    @Override
    public List<Permission> getPermissionListByParentId(Integer parentId ) {
        return permissionMapper.getPermissionListByParentId(parentId);
    }

    @Override
    public List<Permission> getPermissionClassList(List<Permission> permissionList,int maxPermissionLevel) {
        //查找一级权限
        List<Permission> permissionList1 = permissionList.stream().filter(p0 -> p0.getParentId() == 0).collect(Collectors.toList());
        //查找二级权限并筛选出在传来的权限列表中的（角色权限中间表中可能一级权限被删除）
        permissionList1.forEach(p1->{
            if(maxPermissionLevel>=2){
                List<Permission> permissionList2 = getPermissionListByParentId(p1.getId()).stream().filter(permission->permissionList.contains(permission)).collect(Collectors.toList());
                //查找三级权限并筛选出在传来的权限列表中的（角色权限中间表中可能二级权限被删除）
                permissionList2.forEach(p2->{
                    if(maxPermissionLevel>=3){
                        List<Permission> permissionList3 = getPermissionListByParentId(p2.getId()).stream().filter(permission->permissionList.contains(permission)).collect(Collectors.toList());
                        //设置到二级权限的children
                        p2.setChildren(permissionList3);
                    }
                });
                //设置到一级权限的children
                p1.setChildren(permissionList2);
            }
        });

        return permissionList1;
    }

    @Override
    public List<Permission> getPermissionsByRoleId(Integer roleId) {
        return getPermissionClassList(permissionMapper.getPermissionsByRoleId(roleId),3);
    }
}
